# !/usr/bin/env python
# -*- coding: UTF-8 -*-
# author: xzqttt@gmail.com
# date: 07/22/2011
import MySQLdb
import datetime
import pymongo
import simplejson
import MySQLdb
import sys
import pymongo
import connectMongoDB
import connectMySQL

# funcation
def saveToMongo(collection, columns, cursor, sql):
    cursor.execute(sql)
    result_set = cursor.fetchall()
    for result in result_set:
        entity = dict(zip(columns, result))
        try:
            collection.save(entity)
        except :
            pass

page_size = 10000
#db = 'stat'
#tb_list = ['guid_md5']

if len(sys.argv)!=3:
    exit(1)
db = sys.argv[1]
tb_list = [sys.argv[2]]

#mongo_conn = Connection(host, port=port_mongo)
mongo_conn = connectMongoDB.connMongoDB_2()
server_info = mongo_conn.server_info()
print '\n===========Server Info:'
for key in server_info:
    print key, ':', server_info[key]

database = mongo_conn[db]

conn = connectMySQL.connMySQL_stat()
cursor = conn.cursor()

#fetch database
cursor.execute("SHOW TABLES")
tables = cursor.fetchall();

#echo tables
for table in tables:
    tableName = table[0]
    if tableName not in tb_list:
        continue
    collection = database[tableName]
    print '\n=====================' + tableName + '================='
    startTime = datetime.datetime.now()
    print 'startTime is %s' % startTime
    cmd = "DESC " + (tableName)
    cursor.execute(cmd)
    table_desc = cursor.fetchall()
    columns = []
    for desc in table_desc:
        columns.append(desc[0])
    print columns

    sql = "SELECT COUNT(1) FROM %s" % tableName
    cursor.execute(sql)
    total = cursor.fetchone()[0]
    print 'total is %d' % total

    if total > page_size:
        if total % page_size == 0:
            total_page = total / page_size
        else:
            total_page = (total / page_size) + 1
#print 'total_page is %d' % total_page
        page_number = 1
        while page_number <= total_page:
            #print 'current page is %d' % page_number
            sql = "SELECT * FROM %s LIMIT %d,%d" % (tableName, (page_number - 1) * page_size, page_size)
            saveToMongo(collection, columns, cursor, sql)
            page_number += 1
    else:
        #print 'total_page is %d' % 1
        sql = "SELECT * FROM %s LIMIT 1" % tableName
        saveToMongo(collection, columns, cursor, sql)
    endTime = datetime.datetime.now()
    print 'endTime is %s' % endTime
    print 'Time Token : ', endTime - startTime

cursor.close()
conn.close()

print '\nfinished'




